package com.encryption.service.ca.impl;

import java.util.Date;

import com.encryption.dao.CertBindDAO;
import com.encryption.dao.CertDetailDAO;
import com.encryption.model.CertBind;
import com.encryption.model.CertDetail;
import com.encryption.service.ca.CertManageService;

public class CertManageServiceImpl implements CertManageService {
    
    private CertBindDAO certBindDAO = new CertBindDAO();
    private CertDetailDAO certDetailDAO = new CertDetailDAO();
    
    @Override
    public String bindCert(String userId,String certType, String certSn,String thrumbId,String certString) {
        
        CertBind certBind = constructCertBindParam(userId);
        CertDetail certDetail = constructCertDetailParam(certType, certSn, thrumbId, certString);
        certDetail.setUserId(userId);
        try {
            certBindDAO.save(certBind);
            certDetailDAO.save(certDetail);
        } catch (Exception e) {
            return "exception";
        }
        
        return "success";
    }






    @Override
    public String deleteCert(String userId, String certSn) {
        CertDetail certDetail = new CertDetail();
        certDetail.setUserId(userId);
        certDetail.setCertSn(certSn);
        try {
            certDetailDAO.delete(certDetail);
        } catch (Exception e) {
            return "exception";
        }
        return "success";
    }

    @Override
    public String installCert(String userId, String certSn) {
        CertDetail certDetail = new CertDetail();
        certDetail.setUserId(userId);
        certDetail.setCertSn(certSn);
        certDetail.setGmtCreate(new Date());
        certDetail.setGmtModify(new Date());
        try {
            certDetailDAO.save(certDetail);
        } catch (Exception e) {
            return "exception";
        }
        return "success";
    }

    @Override
    public String revoke(String userId) {
        CertDetail certDetail = new CertDetail();
        CertBind certBind = new CertBind();
        certDetail.setUserId(userId);
        certBind.setUserId(userId);
        try {
            certBindDAO.delete(certBind);
            certDetailDAO.delete(certDetail);
        } catch (Exception e) {
            return "exception";
        }
        return "success";
    }
    
    
    private CertDetail constructCertDetailParam(String certType, String certSn, String thrumbId,
                                                String certString) {
        CertDetail certDetail = new CertDetail();
        certDetail.setCertBase64(certString);
        certDetail.setCertSn(certSn);
        certDetail.setCertThrumbid(thrumbId);
        certDetail.setCertType(certType);
        certDetail.setGmtCreate(new Date());
        certDetail.setGmtModify(new Date());
        return certDetail;
    }

    
    
    private CertBind constructCertBindParam(String userId) {
        CertBind certBind = new CertBind();
        certBind.setUserId(userId);
        certBind.setCertStatus("1");
        certBind.setGmtCreate(new Date());
        certBind.setGmtModify(new Date());
        return certBind;
    }
}
